package com.administration.managedBean;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.administration.controller.AdministrationController;
import com.administration.entity.Category;
import com.administration.entity.Privilege;
import com.core.managedBean.BaseBean;

@ManagedBean
@ViewScoped
public class PrivilegeBean extends BaseBean {
	private static final long serialVersionUID = 1L;
	private List<Privilege> privilageList;
	private List<Category> categoryList;
	private Privilege privilege;
	AdministrationController administrationController;;

	@PostConstruct
	public void loadData() {
		if (administrationController == null) {
			administrationController = new AdministrationController();
		}
		privilege = new Privilege();
		loadAllPrivileges();
		loadAllCategories();
	}

	public void savePrivilege() {
		try {
			if (privilege.getRecid() == null) {
				Long privilageRecId;
				privilageRecId = administrationController
						.addPrivilege(this.privilege);
				this.privilege.setRecid(privilageRecId);
			} else {
				administrationController.updatePrivilege(this.privilege);
			}
			loadAllPrivileges();
			super.addInfoMessage("successfully Saved Privilage "
					+ this.privilege.getName());
		} catch (Exception e) {
			super.addErrorMessage("Error Exist");
		}
	}

	public void deletePrivilege() {
		try {
			administrationController.delatePrivilege(this.privilege);
			loadAllPrivileges();
			super.addInfoMessage("successfully Deleted Privilage "
					+ this.privilege.getName());
		} catch (Exception e) {
			super.addErrorMessage("Error Exist");
		}
	}

	public void loadAllPrivileges() {
		try {
			this.privilageList = administrationController.getAllPrivileges();
		} catch (Exception e) {
			super.addErrorMessage("Error Exist");
		}
	}
	public void loadAllCategories() {
		try {
			this.setCategoryList(administrationController.getAllCategories());
		} catch (Exception e) {
			super.addErrorMessage("Error Exist");
		}
	}

	public void toAddPrivilege() {
		this.privilege.setRecid(null);
		this.privilege.setName(null);
	}

	public void toUpdatePrivilege() {
		// this method does't do any thing
		// only to access the beaen
	}

	// --------------------------------------------------------//
	public void setPrivilegeList(List<Privilege> privilageList) {
		this.privilageList = privilageList;
	}

	public List<Privilege> getPrivilegeList() {
		return privilageList;
	}

	public void setPrivilege(Privilege privilege) {
		this.privilege = privilege;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public Privilege getPrivilege() {
		return privilege;
	}

}
